﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace DAL
{
    public class NhaCungCap_DAO
    {
        clsDocSQL m_clsDocSQL = new clsDocSQL();
        
        protected object Getdatarow(DataTable dt, int dong)
        {
            NhaCungCap_DTO dto = new NhaCungCap_DTO();
            dto.Str_MaNCC = dt.Rows[dong]["MaNCC"].ToString();
            dto.Str_TenNCC = dt.Rows[dong]["TenNhaCungCap"].ToString();
            dto.Str_DiaChi = dt.Rows[dong]["DiaChi"].ToString();
            dto.Str_DienThoai = dt.Rows[dong]["DienThoai"].ToString();
            dto.Str_Email = dt.Rows[dong]["Email"].ToString();
            dto.I_TrangThai = int.Parse(dt.Rows[dong]["TrangThai"].ToString());
            return (object)dto;
        }
        public NhaCungCap_DTO[] DanhSachNCC()
        {
            DataTable dt = m_clsDocSQL.DocSQLStoreProcedure("sp_LayDanhNCC");
            NhaCungCap_DTO[] arr;
            arr = new NhaCungCap_DTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                object obj = Getdatarow(dt, i);
                arr[i] = (NhaCungCap_DTO)obj;
            }
            return arr;
        }
        public NhaCungCap_DTO DanhSachNCC_TheoMaNCC(String str_MaNCC)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[1];
            m_SqlParameter[0] = new SqlParameter("@MaNCC", str_MaNCC);
            DataTable dt = m_clsDocSQL.DocSQLStoreProcedure1("sp_LayDanhSachNCC_TheoMaNCC", m_SqlParameter);
            NhaCungCap_DTO arr = new NhaCungCap_DTO();
            object tg = Getdatarow(dt, 0);
            arr = (NhaCungCap_DTO)tg;
            return arr;
        }
        public String MaNCCTang()
        {
            String kq = "";
            kq = m_clsDocSQL.DocKieuString("Ma_NCC_Tang");
            return kq.ToString();
        }
        public int ThemNCC(NhaCungCap_DTO dto)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[7];
            m_SqlParameter[0] = new SqlParameter("@MaNCC", dto.Str_MaNCC);
            m_SqlParameter[1] = new SqlParameter("@TenNCC", dto.Str_TenNCC);
            m_SqlParameter[2] = new SqlParameter("@DiaChi", dto.Str_DiaChi);
            m_SqlParameter[3] = new SqlParameter("@DienThoai", dto.Str_DienThoai);
            m_SqlParameter[4] = new SqlParameter("@Email", dto.Str_Email);
            m_SqlParameter[5] = new SqlParameter("@TrangThai", dto.I_TrangThai);
            m_SqlParameter[6] = new SqlParameter("@KQ", 0);
            m_SqlParameter[6].Direction = ParameterDirection.Output;
            int KQ = m_clsDocSQL.Insert_Delete_Update("sp_ThemNCC", m_SqlParameter);
            return KQ;
        }
        public int XoaNCC(NhaCungCap_DTO dto)//
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[2];
            m_SqlParameter[0] = new SqlParameter("@MaNCC", dto.Str_MaNCC);
            m_SqlParameter[1] = new SqlParameter("@KQ", 0);
            m_SqlParameter[1].Direction = ParameterDirection.Output;
            int kq = m_clsDocSQL.Insert_Delete_Update("sp_XoaNCC", m_SqlParameter);
            return kq;
        }

        public int CapNhatNCC(NhaCungCap_DTO dto)
        {
            SqlParameter[] m_SqlParameter = new SqlParameter[7];
            m_SqlParameter[0] = new SqlParameter("@MaNCC", dto.Str_MaNCC);
            m_SqlParameter[1] = new SqlParameter("@TenNCC", dto.Str_TenNCC);
            m_SqlParameter[2] = new SqlParameter("@DiaChi", dto.Str_DiaChi);
            m_SqlParameter[3] = new SqlParameter("@DienThoai", dto.Str_DienThoai);
            m_SqlParameter[4] = new SqlParameter("@Email", dto.Str_Email);
            m_SqlParameter[5] = new SqlParameter("@TrangThai", dto.I_TrangThai);
            m_SqlParameter[6] = new SqlParameter("@KQ", 0);
            m_SqlParameter[6].Direction = ParameterDirection.Output;
            int kq = m_clsDocSQL.Insert_Delete_Update("sp_CapNhatNCC", m_SqlParameter);
            return kq;
        }
    }
}
